package com.example.worddemo;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WordService.class);

    public static List<Map<String,Object>> getWordTitles() {

        List<Map<String,Object>> list = new ArrayList<>();

        // filePath切换用于测试03和07版本
        String filePath = "src/main/resources/test-2003.doc";
        // String filePath = "src/main/resources/test-2007.docx";

        try {
            InputStream is = new FileInputStream(filePath);

            // 读取docx文件
            if (filePath.substring(filePath.indexOf(".") + 1, filePath.length()).equals("docx")) {

                XWPFDocument doc = new XWPFDocument(is);
                List<XWPFParagraph> paras = doc.getParagraphs();
                for (XWPFParagraph graph : paras) {
                    String text = graph.getParagraphText();
                    String style = graph.getStyle();

                    if(StringUtils.hasLength(text)){
                        // docx的1级标题断点测试从2开始 依次类推

                        if(StringUtils.hasLength(style)){
                            LOGGER.info("段落-------》" +text+ ",标题为--》[" + (Integer.valueOf(style) - 1) + "]级标题");
                        }

                        Map<String,Object> map=new HashMap<>();
                        map.put("text",text);
                        map.put("style",style);
                        list.add(map);
                    }
                }


            } else {

                // 读取doc
                HWPFDocument doc = new HWPFDocument(is);
                Range r = doc.getRange();
                for (int i = 0; i < r.numParagraphs(); i++) {
                    Paragraph p = r.getParagraph(i);

                    // 标题级别
                    short index = p.getStyleIndex();

                    //字号，字号和是否加粗可用来当做标题或者某一关键标识的判断
                    int fontSize = p.getCharacterRun(0).getFontSize();

                    //是否加粗
                    boolean isBold = p.getCharacterRun(0).isBold();

                    // 段落文本
                    String text = p.text().replace("\r","");

                    if(StringUtils.hasLength(text)){

                        // doc 的index
                        if(index!=0){
                            LOGGER.info("段落文本----》"+text+",标题级别为----》"+index+",字体大小----》"+fontSize+",是否加粗----》"+isBold);
                        }

                        Map<String,Object> map=new HashMap<>();
                        map.put("index",index);
                        map.put("fontSize",fontSize);
                        map.put("isBold",isBold);
                        map.put("text",text);
                        list.add(map);

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }
}
